// pages/index/index.js
var utils = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    islogin: false,
    egg: [],
    isimage: true,
    pass: false,
    nickName: '',
    avatarUrl: '../../image/login1.png',
    avatarBase64:'',
    school: '随机',
    daily: 3,
    isdaily: false,
    show: false,
    shows: false,
    actions: [{
        name: '随机',
        subname: '全部类型'
      },
      {
        name: '生活',
        subname: '常识、世界、日常、健康、饮食',
      },
      {
        name: '娱乐',
        subname: '电影、电视、音乐、动漫、游戏',
      }, {
        name: '流行',
        subname: '名人、时尚、体育、商业、科技',
      }, {
        name: '文艺',
        subname: '文化、文学、演艺、艺术、设计',
      }, {
        name: '理科',
        subname: '数学、理化、天文、地理、生物',
      }, {
        name: '文科',
        subname: '语文、外语、历史、经济、哲学',
      },
    ],
    goods: [{
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, {
      title: '复活卡',
      imgurl: '../../image/again.png',
      num: '1'
    }, {
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, {
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, {
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, {
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, {
      title: '答案卡',
      imgurl: '../../image/answer.png',
      num: '1'
    }, ]
  },

  getlogin() {
    wx.login({
      timeout: 20000,
      success: (res) => {
        if (res.code) {
          wx.request({
            url: 'http://127.0.0.1:8089/login',
            method: "POST",
            data: {
              code: res.code,
            },
            success: r => {
              wx.setStorageSync('token', r.header.token)
              this.setData({
                islogin: true
              })
            }
          })
        } else {
          wx.showToast({
            title: "登录失败: " + res.errMsg,
          })
        }
      },
    })
  },
  getAvatar(e) {
    console.log(e);
    var url = e.detail.avatarUrl
    if (url != '') {
      this.setData({
        avatarUrl: url
      })
    }
    wx.getFileSystemManager().readFile({
      filePath: url, //选择图片返回的相对路径
      encoding: 'base64', //编码格式
      success: res => {
        this.setData({
          avatarBase64:res.data
        })
      }
    })
  },
  getname(e) {
    var p = e.detail.pass
    if (p != '') {
      this.setData({
        pass: p,
      })
    }
  },
  setInfo(e){
    const value = e.detail.value
    // utils.http('http://127.0.0.1:8089/userinfo','post',{
    //   username:value.username,
    //   avatarurl:this.data.avatarUrl
    // })
    wx.request({
      url: 'https://api.imgbb.com/1/upload',
      method:"post",
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      data:{
        key:'072b158b3b04467056b5d30b682c421b',
        image:this.data.avatarBase64.toString()
      },
      success:res=>{
        console.log(res.data.data.display_url);
        this.setData({
          avatarUrl:res.data.data.display_url
        })
      }
    })
  },
  tourl(e) {
    var u = e.currentTarget.dataset.u;
    var p = e.currentTarget.dataset.p;
    var url
    const that = this
    if (p == 'a') {
      url = "../../pagesA/" + u + "/" + u + "?" + "school=" + that.data.school;
    } else if (p == 'b') {
      url = "../../pagesB/" + u + "/" + u + "?";
    } else {
      url = "../" + u + "/" + u + '?avatar=' + this.data.avatarUrl;
    }
    wx.navigateTo({
      url: url,
    })
    //   if (this.data.isname && this.data.isimage) {
    //     wx.showToast({
    //       title: '请点击头像登录并输入昵称',
    //       icon: 'none',
    //     })
    //   } else if (this.data.isname) {
    //     wx.showToast({
    //       title: '请输入昵称',
    //       icon: 'none',
    //     })
    //   } else if (this.data.isimage) {
    //     wx.showToast({
    //       title: '请点击头像登录',
    //       icon: 'none',
    //     })
    //   } else {
    //     var u = e.currentTarget.dataset.u;
    //     var url = "../" + u + "/" + u;
    //     wx.navigateTo({
    //       url: url,
    //     })
    //   }
  },

  everyday() {
    this.setData({
      shows: true
    })
  },
  onsheet() {
    this.setData({
      show: true
    })
  },
  onSelect(e) {
    if (e.detail) {
      this.setData({
        show: false,
        school: e.detail.name
      })
      wx.setStorageSync('school', e.detail.name)
    } else {
      this.setData({
        show: false
      })
    }
  },
  onsignHide() {
    this.setData({
      shows: false
    })
  },
  onsign(e) {
    const daily = this.data.daily
    wx.setStorageSync('dailyDate', utils.getDate())
    if (daily < 7) {
      this.setData({
        daily: daily + 1,
        isdaily: true
      })
    } else {
      this.setData({
        daily: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const lastDate = wx.getStorageSync('dailyDate')
    this.setData({
      isdaily: lastDate == utils.getDate()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    utils.http('https://v.api.aa1.cn/api/time-tx/index.php').then(res => {
      this.setData({
        egg: [res.data.msg],
      })
      utils.http('https://zj.v.api.aa1.cn/api/wenan-mj/?type=json').then(res => {
        this.setData({
          egg: this.data.egg.concat(res.data.msg.split('——'))
        })
      })
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})